﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Core;
using System.Configuration;
using System.Reflection;

namespace Ashlen.Portal.Data.SqlServer
{
    public class BaseDao
    {
        private PORTAL_Entities _context = null;
        private string _connectionString = string.Empty;

        protected BaseDao()
        {
            _context = new PORTAL_Entities();
        }
         protected BaseDao(string connectionString) {
             _connectionString = connectionString;
             _context = new PORTAL_Entities(_connectionString);
        }

        protected PORTAL_Entities Context
        {
            get
            {
                return _context;
            }
        }
        protected void RenewContext()
        {
             if (_connectionString == string.Empty)
             {
                 _connectionString = ConfigurationManager.ConnectionStrings["PORTAL_Entities"].ConnectionString;
                 _context = new PORTAL_Entities(_connectionString);
             }
             _context = new PORTAL_Entities(_connectionString);
            return;
        }

        /// <summary>
        /// Since this method has not been exposed on the entities object, we shall
        /// use reflection to expose it
        /// </summary>
        protected void ClearCache()
        {
            _context.GetType().InvokeMember("ClearCache", 
            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
            null, _context, null); 
        }
    }
}
